package com.tanglongan.concurrent._sync;

import lombok.AllArgsConstructor;

public class Sync03 {

    public static void main(String[] args) {
        Sync03Service service = new Sync03Service();
        Thread t1 = new Sync03ThreadA(service);
        Thread t2 = new Sync03ThreadB(service);

        t1.start();
        t2.start();
    }
}

class Sync03Service {
    synchronized public void foo1() {
        System.out.println("开始运行foo1方法，threadName=" + Thread.currentThread().getName());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("foo1方法运行结束");
    }

    synchronized public void foo2() {
        System.out.println("开始运行foo2方法，threadName=" + Thread.currentThread().getName());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("foo2方法运行结束");
    }
}

@AllArgsConstructor
class Sync03ThreadA extends Thread {
    private Sync03Service service;

    @Override
    public void run() {
        service.foo1();
    }
}

@AllArgsConstructor
class Sync03ThreadB extends Thread {
    private Sync03Service service;

    @Override
    public void run() {
        service.foo2();
    }
}